import { login } from '@/apis/login'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref({
      id: '',
      account: '',
      mobile: '',
      token: '',
      avatar: '',
      nickname: '',
      gender: '',
      birthday: '',
      cityCode: '',
      provinceCode: '',
      profession: '',
    })
    const getUserInfo = async (loginParam) => {
      if (userInfo.value.token) {
        return userInfo.value
      }
      userInfo.value = await login(loginParam)
      return userInfo.value
    }

    const delUserInfo = () => {
      userInfo.value = {
        id: '',
        account: '',
        mobile: '',
        token: '',
        avatar: '',
        nickname: '',
        gender: '',
        birthday: '',
        cityCode: '',
        provinceCode: '',
        profession: '',
      }
    }
    return {
      userInfo,
      getUserInfo,
      delUserInfo,
    }
  },
  {
    persist: {
      key: 'userInfo',
      storage: sessionStorage,
      paths: ['userInfo'],
    },
  },
)
